"""
public function
"""
import os
import random

from yat.test import macro

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Common:
    def __init__(self, node_name='dbuser'):
        self.logger = Logger()
        self.Constant = Constant()
        self.sh_primy = CommonSH(node_name)

    def console(self, msg):
        """
        打印关键日志
        :param msg: 需要打印的信息
        """
        self.logger.info("---- {} ----".format(msg))

    def scp_file(self, node, file_name, target_path, del_sign=False):
        """
        传输当前主机上的文件到node所在主机target_path下
        :param node: 目标节点
        :param file_name: 文件名
        :param target_path: 目标主机上的路径
        :param del_sign: 删除标志位
        :return:
        """
        filepath_1 = os.path.join(macro.SCRIPTS_PATH, file_name)
        newsql_path1 = os.path.join(target_path, file_name)

        if not del_sign:
            mkdir_cmd = f'if [ ! -d {target_path} ];' \
                        f'then mkdir {target_path};fi'
            self.console(mkdir_cmd)
            msg = node.sh(mkdir_cmd).result()
            self.console(msg)

            self.console('filepath_1:' + filepath_1)
            self.console('newsql_path1:' + newsql_path1)

            cmd = f'rm -rf {newsql_path1}'
            self.console(cmd)
            msg = node.sh(cmd).result()
            self.console(msg)
            node.scp_put(filepath_1, newsql_path1)
        else:
            rm_sqlfile = f'rm -rf {newsql_path1}'
            self.console(rm_sqlfile)
            msg = node.sh(rm_sqlfile).result()
            self.console(msg)

    def execute_jdbc(self, conf_list, jdbc_dir, java_file, node, extjar=False):
        text = '--------1.写配置文件-------'
        self.logger.info(text)
        self.scp_file(node, f"{java_file}.java", jdbc_dir)
        property_file = os.path.join(jdbc_dir, "prop.properties")
        result = node.sh(f"rm -rf {property_file} && \
                         touch {property_file}").result()
        self.logger.info(result)
        result = node.sh(f"touch {property_file}").result()
        self.logger.info(result)
        for conf in conf_list:
            config = f'echo "{conf}={conf_list[conf]}">> {property_file}'
            node.sh(config)
        conf_content = node.sh(f"cat {property_file}").result()
        self.logger.info(conf_content)

        for conf in conf_list:
            assert (f"{conf}=" in conf_content)

        text = '--------------2. 编译java文件------------------'
        self.logger.info(text)
        node.scp_put(macro.JDBC_PATH, f"{jdbc_dir}/opengauss.jar")
        if extjar:
            self.logger.info("put extjar")
            node.scp_put(macro.BCPROV_PATH, f"{jdbc_dir}/bcprov.jar")
        cmd = f"javac -encoding utf-8 -cp " \
            f'"{jdbc_dir}/*" ' \
            f"{os.path.join(jdbc_dir, f'{java_file}.java')}"
        self.logger.info(cmd)
        result = node.sh(cmd).result()
        self.logger.info(result)

        text = '-------------3.运行java文件---------------------'
        self.logger.info(text)
        cmd = f' java -cp "{jdbc_dir}/*"' \
            f":{jdbc_dir} {java_file} -F {property_file}"
        self.logger.info(cmd)
        result = node.sh(cmd).result()
        self.logger.info(result)
        return result